package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.pojo.dto.DepartmentListDTO;
import com.xbongbong.paas.pojo.vo.DepartmentListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.feedback.pojo.dto.FeedbackDTO;
import com.xbongbong.pro.feedback.pojo.vo.FeedbackVO;
import com.xbongbong.saas.domain.entity.FeedbackEntity;
import com.xbongbong.saas.model.FeedbackModel;
import com.xbongbong.saas.service.FeedbackService;
import com.xbongbong.sys.model.DepartmentModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chy
 * @time 2019-02-22 13:34
 */
@Service("feedbackService")
public class FeedbackServiceImpl implements FeedbackService {

    private static final Logger LOG = LoggerFactory.getLogger(FeedbackServiceImpl.class);

    @Resource
    private DepartmentModel departmentModel;
    @Resource
    private FeedbackModel feedbackModel;

    public DepartmentListVO list(DepartmentListDTO departmentListDTO) throws XbbException {
        DepartmentListVO departmentListVO = new DepartmentListVO();
        String corpid = departmentListDTO.getCorpid();
        Boolean showHidden = departmentListDTO.getShowHidden();

        departmentListVO.setCorpid(corpid);

        try {
            JSONArray departmentTree = departmentModel.getDepartmentJsonTree(corpid, showHidden);
            departmentListVO.setDepartmentTree(departmentTree);
        } catch (Exception e) {
            LOG.error("departmentService.list 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return departmentListVO;
    }


    @Override
    public FeedbackVO save(FeedbackDTO feedbackDTO) throws XbbException {
        FeedbackVO feedbackVO = new FeedbackVO();

        String corpid = feedbackDTO.getCorpid();
        String userId = feedbackDTO.getUserId();

        String content = feedbackDTO.getContent();
        String contactInfo = feedbackDTO.getContactInfo();

        //contactInfo格式校验，手机号或邮箱
        boolean isMobile = StringUtil.isMobile(contactInfo);
        boolean isEmail = StringUtil.isEmail(contactInfo);

        //获取手机号或邮箱
        String phone = "";
        if (isMobile) {
            phone = contactInfo;
        }
        String email = "";
        if (isEmail) {
            email = contactInfo;
        }
        String otherWays = "";
        if (!isMobile && !isEmail) {
            otherWays = contactInfo;
        }

        List<String> images = feedbackDTO.getImages();
        JSONArray files = feedbackDTO.getFiles();
        if (images == null) {
            images = new ArrayList<>();
        }

        if(files == null){
            files = new JSONArray();
        }




        FeedbackEntity feedback = new FeedbackEntity();
        feedback.setUserId(userId);
        feedback.setCorpid(corpid);
        feedback.setContent(content);
        feedback.setPhone(phone);
        feedback.setEmail(email);
        feedback.setOtherWays(otherWays);
        feedback.setImages(images.toString());
        feedback.setFiles(files.toJSONString());
        try {
            feedbackModel.insert(feedback);
        } catch (XbbException e) {
            LOG.error("feedbackModel.insert error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }

        return feedbackVO;
    }
}
